package com.qibo.receipt;

import android.content.Intent;
import android.os.RemoteException;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hjq.toast.ToastUtils;
import com.qibo.baselib.base.BaseActivity;
import com.qibo.baselib.utils.PrinterManagerUtil;
import com.qibo.receipt.adapter.PayKeyAdapter;
import com.sunmi.peripheral.printer.InnerPrinterCallback;
import com.sunmi.peripheral.printer.InnerPrinterException;
import com.sunmi.peripheral.printer.InnerPrinterManager;
import com.sunmi.peripheral.printer.InnerResultCallbcak;
import com.sunmi.peripheral.printer.SunmiPrinterService;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/receiptmodule/ReceiptMainActivity")
public class ReceiptMainActivity extends BaseActivity implements View.OnClickListener{
    private static String Tag = ReceiptMainActivity.class.getName();
    private static final int REQUEST_CODE_QRCODE_PERMISSIONS = 1;
    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    //键盘
    private GridView gridview;
    private List<String> numList = new ArrayList<String>();
    private int numIndex=0;
    private String EditTxtValue="";
    private EditText editMemberAmount;
    private ImageButton btnBack_pay1;
    private TextView textView_titleBar;
    private LinearLayout receiptmodule_main_ll_pay;
    private LinearLayout receiptmodule_main_ll_cash;
    private LinearLayout receiptmodule_main_ll_swipe;
    private SunmiPrinterService sunmiPrinterService;
    @Override
    protected int getContentViewId() {
        return R.layout.receiptmodule_activity_main;
    }

    @Override
    protected void initView() {
        editMemberAmount = findViewById(R.id.editMemberAmount);
        editMemberAmount.setInputType(InputType.TYPE_NULL);
        gridview = findViewById(R.id.gridview);
        btnBack_pay1 = findViewById(R.id.btnBack_pay1);
        textView_titleBar = findViewById(R.id.textView_titleBar);
        receiptmodule_main_ll_pay = findViewById(R.id.receiptmodule_main_ll_pay);
        receiptmodule_main_ll_cash = findViewById(R.id.receiptmodule_main_ll_cash);
        receiptmodule_main_ll_swipe = findViewById(R.id.receiptmodule_main_ll_swipe);

    }

    @Override
    protected void initAction() {
        textView_titleBar.setText("收银台");
        setUpPayKeyAdapter();
        btnBack_pay1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        receiptmodule_main_ll_pay.setOnClickListener(this);
        receiptmodule_main_ll_cash.setOnClickListener(this);
        receiptmodule_main_ll_swipe.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    private void setUpPayKeyAdapter() {
        final PayKeyAdapter adapter = new PayKeyAdapter(this);
        gridview.setAdapter(adapter);
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String keyType = view.findViewById(R.id.GridTextView).getTag().toString();
                String textValue = ((TextView)view.findViewById(R.id.GridTextView)).getText().toString();
                String editText = editMemberAmount.getText().toString();
                //CalculateUtils.calculate(mContext,keyType,textValue,editText,numList,numIndex,editMemberAmount);
                if(keyType.equals("num")){
                    //数字键
                    try{
                        String orgValue = numList.get(numIndex);
                        if(editText.equals("0")){
                            editMemberAmount.setText(textValue);
                        }else{
                            //小数点最多只能后两位
                            if(orgValue.equals("0.00")){
                                orgValue=textValue;
                                numList.set(numIndex, textValue);
                                editMemberAmount.setText(textValue);
                                return;
                            }
                            if(orgValue.lastIndexOf(".")!=-1 && orgValue.lastIndexOf(".")<orgValue.length()-2){
                                Toast.makeText(mContext,"只能输入小数点后两位",Toast.LENGTH_SHORT).show();
                            }else{
                                if(editText.length()>=18){
                                    Toast.makeText(mContext,"一次输入算式无法超过18个字符",Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                int numLen = orgValue.length();
                                if(orgValue.lastIndexOf(".")!=-1){
                                    numLen = orgValue.lastIndexOf(".")+1;
                                }
                                if(numLen==7){
                                    Toast.makeText(mContext,"收款金额不能超过7位数",Toast.LENGTH_SHORT).show();
                                }else{
                                    numList.set(numIndex, orgValue+textValue);
                                    editMemberAmount.setText(editText+textValue);
                                }
                            }
                        }
                    }catch(Exception e){
                        numIndex=0;
                        numList=new ArrayList<String>();
                        numList.add(textValue);
                        editMemberAmount.setText(textValue);
                    }
                }
                if(keyType.equals("back")){
                    //退格
                    try{
                        if(editText.lastIndexOf("+") == editText.length()-1){
                            //最后为加则减少一位
                            numList.set(numIndex,"");
                            numIndex--;
                            editMemberAmount.setText(editText.substring(0,editText.length()-1));
                        }else{
                            String orgValue = numList.get(numIndex);
                            if(!orgValue.equals("")){
                                numList.set(numIndex, orgValue.substring(0,orgValue.length()-1));
                            }
                            if(editText.equals("0")||editText.length()==1){
                                editMemberAmount.setText("0.00");
                                numList.set(numIndex,"0.00");
                            }else{
                                editMemberAmount.setText(editText.substring(0,editText.length()-1));
                            }
                        }
                    }catch(Exception e){
                        numIndex=0;
                        numList=new ArrayList<String>();
                        numList.add("0");
                        editMemberAmount.setText("");
                    }
                }
                if(keyType.equals("point")){
                    //小数点
                    try{
                        String orgValue = numList.get(numIndex);
                        if(orgValue.lastIndexOf(".")!=-1){
                            Toast.makeText(mContext,"已存在小数点",Toast.LENGTH_SHORT).show();
                        }else{
                            if(editText.lastIndexOf("+") == editText.length()-1){
                                //最后为加则补0
                                numList.set(numIndex, orgValue+"0"+textValue);
                                editMemberAmount.setText(editText+"0"+textValue);
                            }else{
                                numList.set(numIndex, orgValue+textValue);
                                editMemberAmount.setText(editText+textValue);
                            }
                        }
                    }catch(Exception e){
                        numIndex=0;
                        numList=new ArrayList<String>();
                        numList.add("0.");
                        editMemberAmount.setText("0.");
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        String editText = editMemberAmount.getText().toString();
        int i = view.getId();
        if (i == R.id.receiptmodule_main_ll_pay) {
            if(null==editText ||"".equals(editText) || Double.valueOf(editText)<=0){
                ToastUtils.show("请输入收款金额！");
                return;
            }
            Intent intent = new Intent(this,PayMainFragmentActivity.class);
            intent.putExtra("amount",editText);
            startActivity(intent);
        } else if (i == R.id.receiptmodule_main_ll_cash) {
            if(null==editText || "".equals(editText)|| Double.valueOf(editText)<=0){
                ToastUtils.show("请输入收款金额！");
                return;
            }
            Intent intent = new Intent(this,CashReceiptActivity.class);
            intent.putExtra("amount",editText);
            startActivity(intent);
        } else if (i == R.id.receiptmodule_main_ll_swipe) {
            new PrinterManagerUtil(mContext);
        }
    }
    @Override
    protected void onStart() {
        super.onStart();
        editMemberAmount.setText("");
    }
}
